﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bookkeeping.Core.Entities;
using Bookkeeping.Core.Services;
using Bookkeeping.Mvc.Extensions;
using System.Web.Mvc;

namespace Bookkeeping.Mvc.Models.BudgetPost
{
    public class BindablePost
    {
        public int Id { get; set; }
        public double Amount { get; set; }
        public string Comment { get; set; }
        public string Name { get; set; }
        public PostType Type { get; set; }
        public string Tags { get; set; }

        public static BindablePost FromPost(Post post)
        {
            return new BindablePost()
            {
                Id = post.Id,
                Name = post.Name,
                Type = post.Type,
                Comment = post.Comment,
                Amount = post.Amount,
                Tags = post.Tags.ToSpaceSeparatedString()
            };
        }

        private static string CreateFlattenedTagList(Post post)
        {
            string tags = "";

            foreach (var tag in post.Tags)
                tags += tag.Name + " ";

            return tags.Trim();
        }

        public Post ToPost()
        {
            var post = new Post()
            {
                Id = this.Id,
                Name = this.Name,
                Type = this.Type,
                Comment = this.Comment,
                Amount = this.Amount
            };

            var tagNames = Tags.Split(' ');

            foreach (var tagName in tagNames)
                post.AddTag(new Tag(tagName));

            return post;
        }

        public IEnumerable<SelectListItem> TypeListItems
        {
            get
            {
                return new List<SelectListItem>()
                {
                    new SelectListItem()
                    {
                        Text = "Income",
                        Value = "Income",
                        Selected = Type == PostType.Income
                    },
                    new SelectListItem()
                    {
                        Text = "Expense",
                        Value = "Expense",
                        Selected = Type == PostType.Expense
                    },
                    new SelectListItem()
                    {
                        Text = "Saving",
                        Value = "Saving",
                        Selected = Type == PostType.Saving
                    }
                };
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return false;
            }

            BindablePost other = (BindablePost)obj;

            return Id == other.Id &&
                Name == other.Name &&
                Comment == other.Comment &&
                Amount == other.Amount &&
                Type == other.Type &&
                Tags == other.Tags;
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }
}